
package com.example.run_game;
import android.location.Location;


public class Checkpoint {
	public double radius;
	public String description;
	public String name;
	public Location location;
	public int counter =0;
	public int lastVisited =0;

	Checkpoint(double radIn, String descIn, String nameIn, Location loc)
	{
		radius = radIn;
		description = descIn;
		name = nameIn;
		location = loc;
	}
	
	public int getStraightDistTo(Checkpoint goal)
	{
	    float res = location.distanceTo(goal.location);
		return (int)res;
	}
	
	public String getLocationString() {
	    return Location.convert(location.getLatitude(), Location.FORMAT_DEGREES) + "," + Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
	}
	
	public boolean isViableChoice(Checkpoint current, Checkpoint start, double upperBound)
	{		
		int distToStartVia = getDistTo(current) + getDistTo(start);
		if(distToStartVia < upperBound && distToStartVia > 25)
			return true;
		else 
		    return false;
	}
	
	public double getWeight(int ckpsVisited, Checkpoint current)
	{
		//smaller the number the more recently visited
		int visitedAgo = ckpsVisited - lastVisited + 1;
		double distanceToCurrent = getDistTo(current);
		return distanceToCurrent / visitedAgo;
	}
	
	//returns the best distance estimate possible
	public int getDistTo(Checkpoint goal)
	{
		int dist = -1;		
		String urlS = "http://maps.googleapis.com/maps/api/distancematrix/json?" +
		"origins=" +
		this.getLocationString() + 
		"|&destinations=" +
		goal.getLocationString() + 
		"|&mode=walking&language=en-EN&sensor=false";
		//TODO put the async task in here
		if(dist == -1)
			dist = getStraightDistTo(goal);
		return dist;
	}
	
/*	private class ASyncDistanceQuery extends AsyncTask<String, Void, int>
	{
	     protected int doInBackground(String request)
	     {
	    	 String reply = ""; 	
	    	 int dist = -1;	
	    	 try 
	    	 {
				URL url = new URL(request);	 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		        while ((reply += reader.readLine()) != null) {} 	 
		        reader.close();
				JSONObject gMapDistQuery = new JSONObject(reply);
				dist = gMapDistQuery.getJSONObject("rows").getJSONObject("distance").getInt("value");
			 }
			 catch (MalformedURLException e) {
	             e.printStackTrace();
	         } catch (IOException e) {
	             e.printStackTrace();
	         } catch (JSONException e) {
		 		e.printStackTrace();
	 		 }	
 			 catch (android.os.NetworkOnMainThreadException e) {
				e.printStackTrace();
			 }	
	    	 return dist;
	     }	     	     

	     protected int onPostExecute(int result) {
	         showDialog("Downloaded " + result + " bytes");
	     }
	 }
	 */
	
}
